package com.ssm.controller;

import com.ssm.pojo.Repair;
import com.ssm.pojo.Student;
import com.ssm.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    @Qualifier(value = "StudentServiceImpl")
    private  StudentService studentService;
    @Autowired
    @Qualifier("managerServiceImpl")
    private ManagerService managerService;
    @Autowired
    @Qualifier("repairServiceImpl")
    private RepairService repairService;
    @Autowired
    @Qualifier("NoticeServiceImpl")
    private NoticeService noticeService;

    @RequestMapping("/check")
    public String check(int sId, String sPassword,HttpSession session, Model model){
        System.out.println(sId+ "密码是"+sPassword);
        Student student = studentService.queryStudentBySid(sId);
        System.out.println(student);
        if(student==null){
//            return "redirect:/index.jsp";
//            return "redirect:http://localhost:8080/dormSystemManager/index.jsp";
            return "index";
        }
        if(sPassword.equals(student.getsPassword()))
        {

            session.setAttribute("student",student);
            return "student";
        }

//        return "redirect:http://localhost:8080/dormSystemManager/index.jsp";
        return "index";
    }
    @RequestMapping("/queryAllManager")
    public ModelAndView queryAllManager(){

        ModelAndView mv = new ModelAndView();
        List mlist = managerService.queryAllManger();
        System.out.println(mlist);
        mv.addObject("mlist",mlist);
        mv.setViewName("student");
        return mv;
    }
    @RequestMapping("queryStudentBySid")
    public ModelAndView queryStudentBySid(HttpSession session){
        Student student = (Student) session.getAttribute("student");
        int sId=student.getsId();
        System.out.println(sId);
        Student student1 = studentService.queryStudentBySid(sId);
        System.out.println(student1);
        ModelAndView mv=new ModelAndView();
        mv.addObject("student1",student1);
        mv.setViewName("student");
        return mv;
    }
    @RequestMapping("/toUpdateStudent")
    public String toUpdateStudent(){
        return "updateStudent";
    }
    @RequestMapping("/updateStudent")
    public ModelAndView updateStudent(HttpSession session,String sName,String sPassword,String sCollege,String sProfession,String sPhone){
        Student student = (Student) session.getAttribute("student");
        student.setsName(sName);
        student.setsPassword(sPassword);
        student.setsCollege(sCollege);
        student.setsProfession(sProfession);
        student.setsPhone(sPhone);
        int i = studentService.updateStudentBySId(student);
        ModelAndView mv = new ModelAndView();
        if (i>0){
            mv.addObject("msg","学生信息修改成功");
            mv.setViewName("student");
            return mv;
        }else {
            mv.addObject("msg","学生信息修改失败");
            mv.setViewName("student");
            return mv;

        }

    }
    @RequestMapping("/checkNotice")
    public String checkNotice(){
        return "submitNotice";
    }
    @RequestMapping("/submitRepair")
    public ModelAndView submitRepair(String rDetail){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String rDate = simpleDateFormat.format(new Date());
        Repair repair = new Repair();
        repair.setrDetail(rDetail);
        repair.setrDate(rDate);
        int i = repairService.addRepair(repair);
        ModelAndView mv = new ModelAndView();
        if(i>0){
          mv.addObject("rmsg","提交保修单成功");
          mv.setViewName("student");
          return mv;
        }else {
            mv.addObject("rmsg","提交报修单失败");
            mv.setViewName("student");
            return mv;
        }

    }
    @RequestMapping("/queryNoticePage")
    public String queryNoticePage(){
        return "queryNoticePage";
    }
    @RequestMapping("/queryNotice")
    public ModelAndView queryNotice(String nDate){
        ModelAndView mv = new ModelAndView();
        List list = noticeService.queryNotice(nDate);
        if(list.size()==0){
            mv.addObject("nmsg","没有公告");
            mv.setViewName("student");
            return mv;
        }else {
            mv.addObject("list", list);
            mv.setViewName("student");
            return mv;
        }
    }
}
